﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wechat.Util.Request;
using Wechat.Util.StateCode;

namespace Wechat.Util.Auth
{
  public  class AuthOperater
    {
        public AuthResult AuthOperaterIsLicit(AuthOptRequest authOptRequest)
        {
            AuthResult authResult = new AuthResult();
            if (String.IsNullOrEmpty(authOptRequest.OptCookie))
            {
                authResult.AuthStatus = EnumAuthStatus.NoLogin;
                authResult.Message = "没有登录";
                return authResult;
            }
            OperatorCookie model =new AuthOperaterHelper().DESDecryptCookieInfo(authOptRequest.OptCookie);
            if (model == null)
            {
                authResult.AuthStatus = EnumAuthStatus.DecryptError;
                authResult.Message = "Cookie非法,解析错误!";
                return authResult;
            }
            //解析Cookie成功
            //每天都需要登陆一次
            if (!DateTime.Now.ToShortDateString().Equals(model.LoginTime.ToShortDateString()))
            {
                authResult.AuthStatus = EnumAuthStatus.AgainLogin;
                authResult.Message = "认证过期,需要重新登录!";
                return authResult;
            }
            //判断是否具有访问权限
            if (new AuthOperaterHelper().HasPermission(authOptRequest, model))
            {
                authResult.AuthStatus = EnumAuthStatus.Success;
                authResult.Message = "认证通过";
                return authResult;
            }
            else
            {
                authResult.AuthStatus = EnumAuthStatus.NoPermission;
                authResult.Message = "没有权限";
                return authResult;
            }
        }
    }
}
